//
//  DSZDiZhiXianShiViewContrller.m
//  7.13苏宁框架
//
//  Created by app05 on 15/7/26.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZDiZhiXianShiViewContrller.h"
#import "DSZDiZhiGLViewController.h"
#import "DSZMoreView.h"
#import "DSZDiZhICell.h"
#import "UserDB.h"
#import "UserModel.h"
#import "DSZDiZhiBJViewController.h"

#import "DSZMainViewController.h"
#import "HomeViewController.h"
#import "DSZShopViewController.h"
@interface DSZDiZhiXianShiViewContrller ()<UITableViewDataSource,UITableViewDelegate,tableViewIndexPathRowDelegate>
- (IBAction)addLocation:(id)sender;

@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *data;


@property(nonatomic,weak)UIView *moreView;
@property(nonatomic,assign)BOOL flag;
@end

@implementation DSZDiZhiXianShiViewContrller


-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.alpha=0;
    [self loadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UserDB shareInstance] creatTable];
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 480-64-38)];
    
    tableView.dataSource=self;
    tableView.delegate=self;
    
    self.tableView=tableView;
    
    [self.view addSubview:tableView];
}

- (void)loadData {
    //查询数据
    NSArray *arr=[NSArray array];
    arr= [[UserDB shareInstance] findUsers];
    self.data=[NSMutableArray arrayWithArray:arr];
    //[self.data addObject:arr];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    
    DSZDiZhICell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [DSZDiZhICell cell];
    }
    
    UserModel *user = [self.data objectAtIndex:indexPath.row];
    cell.model=user;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DSZDiZhiBJViewController *BJ=[[DSZDiZhiBJViewController alloc]init];
    [self.navigationController pushViewController:BJ animated:YES];
    UserModel *user = [self.data objectAtIndex:indexPath.row];
    BJ.model=user;
}

#pragma mark - 可编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UserModel *user = [self.data objectAtIndex:indexPath.row];
        [self.data removeObjectAtIndex:indexPath.row];
        
        [[UserDB shareInstance] deletedUser:user];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
    }
}


- (IBAction)back {
    //DSZMainHeaderView *main=[[DSZMainHeaderView alloc]init];
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)more {
    if (!self.flag) {
        DSZMoreView *more=[DSZMoreView moreView];
        more.delegate=self;
        more.frame=CGRectMake(185, 65, 146, 186);
        self.moreView=more;
        [self.view addSubview:self.moreView];
        self.flag=YES;
    }
    else
    {
        [self.moreView removeFromSuperview];
        self.moreView=nil;
        self.flag=NO;
    }
    
}
- (IBAction)addLocation:(id)sender {
    DSZDiZhiGLViewController *dzgl=[[DSZDiZhiGLViewController alloc]init];
    [self.navigationController pushViewController:dzgl animated:YES];

}
-(void)dszMoreViewWithIndexRow:(NSInteger)indexPathRow
{
    if (indexPathRow==0) {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DSZMainViewController *main=[story instantiateViewControllerWithIdentifier:@"main"];
        //DSZMainViewController *main=[[DSZMainViewController alloc]init];
        [self presentViewController: main animated:NO completion:nil];
    }else if (indexPathRow==1)
    {}
    else if (indexPathRow==2)
    {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        DSZShopViewController *shop=[story instantiateViewControllerWithIdentifier:@"shop"];
        [self.navigationController pushViewController:shop animated:NO];
        self.flag=YES;
        [self more];
        self.navigationController.navigationBar.alpha=1;
        DSZMainViewController *main=(DSZMainViewController*)self.tabBarController;
        [main hiddenMainTabbar:YES];
    }else if (indexPathRow==3)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    
}
@end
